﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DaoShopOnline;
using System.Data.SqlClient;

namespace ManageCategoriesProducts
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private ManageCategories daoCategories;
        private ManageProducts daoProducts;

        public static readonly DependencyProperty CategoryNameProperty =
            DependencyProperty.Register("CategoryName", typeof(string), typeof(DataGrid));

        public MainWindow()
        {
            InitializeComponent();

            BindCategories();
            BindProducts();
        }

        private void BindProducts()
        {
            this.daoProducts = new ManageProducts();
            dataGridProducts.ItemsSource = this.daoProducts.GetAllProducts();
        }

        private void BindCategories()
        {
            this.daoCategories = new ManageCategories();
            dataGridCategories.ItemsSource = daoCategories.GetAllCategories();
        }

        private void buttonDeleteCategories_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Category category = (Category)dataGridCategories.SelectedItem;
                ConfirmWindow confirm = new ConfirmWindow();
                confirm.ShowDialog();
                if (confirm.Delete)
                {
                    this.daoCategories.DeleteCategory(category);
                    BindCategories();
                }
            }
            catch (SqlException ex)
            {
                ErrorWindow errorWindow = new ErrorWindow("Cannot delete category!", ex.Message);
                errorWindow.ShowDialog();
            }
        }

        private void buttonEditCategory_Click(object sender, RoutedEventArgs e)
        {
            Category category = (Category)dataGridCategories.SelectedItem;
            EditCategoryWindow editCategoryWindow = new EditCategoryWindow(category);
            editCategoryWindow.ShowDialog();
            if (editCategoryWindow.DialogResult == true)
            {
                this.daoCategories.EditCategory(editCategoryWindow.CategoryToEdit);
            }
            else
            {
                BindCategories();
            }
        }

        private void buttonAddCategory_Click(object sender, RoutedEventArgs e)
        {
            AddCategoryWindow addCategoryWindow = new AddCategoryWindow();
            addCategoryWindow.ShowDialog();
            if (addCategoryWindow.DialogResult == true)
            {
                this.daoCategories.AddCategory(addCategoryWindow.newCategory);
                BindCategories();
            }
        }

        private void buttonAddProduct_Click(object sender, RoutedEventArgs e)
        {
            AddProductWindow addProductWindow = new AddProductWindow();
            addProductWindow.ShowDialog();
            if (addProductWindow.DialogResult == true)
            {
                this.daoProducts.AddProduct(addProductWindow.newProduct);
                BindProducts();
            }
        }

        private void buttonDeleteProduct_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Product product = (Product)dataGridProducts.SelectedItem;
                ConfirmWindow confirm = new ConfirmWindow();
                confirm.ShowDialog();
                if (confirm.Delete)
                {
                    this.daoProducts.DeleteProduct(product);
                    BindProducts();
                }
            }
            catch (SqlException ex)
            {
                ErrorWindow errorWindow = new ErrorWindow("Cannot delete category!", ex.Message);
                errorWindow.ShowDialog();
            }
        }

        private void buttonEditProduct_Click(object sender, RoutedEventArgs e)
        {
            Product product = (Product)dataGridProducts.SelectedItem;
            EditProductWindow editProductWindow = new EditProductWindow(product);
            editProductWindow.ShowDialog();
            if (editProductWindow.DialogResult == true)
            {
                this.daoProducts.EditProduct(editProductWindow.ProductToEdit);
            }
            else
            {
                BindProducts();
            }
        }
    }
}
